/*************************************************************************

Copyright 2010 Isuru Udana Loku Narangoda

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

**************************************************************************/



package remainingdegreedistribution;

/**
 *
 * @author ISURU UDANA LOKU NARANGODA
 * isudana@gmail.com
 * 
 */

import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;

public class Graph extends JPanel {
    private double[] hvalues;
    private String[] xAxisLabels;
    

    public Graph(double[] hValues, String[] xAxisLabels) {
        this.xAxisLabels = xAxisLabels;
        this.hvalues = hValues;
    }

    public void paintComponent(Graphics graphic) {
        super.paintComponent(graphic);
        if (hvalues == null || hvalues.length == 0)
            return;
        double minValue = 0;
        double maxValue = 0;

        for (int i = 0; i < hvalues.length; i++) {
            if (minValue > hvalues[i])
                minValue = hvalues[i];
            if (maxValue < hvalues[i])
                maxValue = hvalues[i];
        }

        Dimension d = getSize();
        int clientWidth = d.width;
        int clientHeight = d.height;
        int barWidth = clientWidth / hvalues.length;

      
        // Label
        Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
        FontMetrics labelFontMetrics = graphic.getFontMetrics(labelFont);

        int top = 15;
        int bottom = labelFontMetrics.getHeight();
        if (maxValue == minValue) {
            return;
        }
        double scale = (clientHeight - top - bottom) / (maxValue - minValue);
        int y = clientHeight - labelFontMetrics.getDescent();
        graphic.setFont(labelFont);

        for (int i = 0; i < hvalues.length; i++) {
            int valueX = i * barWidth + 1;
            int valueY = top;
            int height = (int) (hvalues[i] * scale);
            if (hvalues[i] >= 0)
                valueY += (int) ((maxValue - hvalues[i]) * scale);
            else {
                valueY += (int) (maxValue * scale);
                height = -height;
            }

            graphic.setColor(Color.BLUE);
            graphic.fillRect(valueX, valueY, barWidth - 2, height);
            graphic.setColor(Color.black);
            DecimalFormat df = new DecimalFormat("#.###");
            int labelWidth = labelFontMetrics.stringWidth(df.format(hvalues[i]));
            int  x = i * barWidth + (barWidth - labelWidth) / 2;
            graphic.drawString(df.format(hvalues[i]), x, (valueY - 2));
            graphic.drawRect(valueX, valueY, barWidth - 2, height);
            labelWidth = labelFontMetrics.stringWidth(xAxisLabels[i]);
            x = i * barWidth + (barWidth - labelWidth) / 2;
            graphic.drawString(xAxisLabels[i], x, y);
        }
    }


}